package com.nlscan.ncomgateway.core.database.model

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import com.nlscan.ncomgateway.core.model.entity.Device

/**
 * <pre>
 *  author: lin.qing
 *  time  : 2023/3/21
 *  desc  : 设备 数据表
 * </pre>
 */
@Entity(
    tableName = "tb_device",
    indices = [Index(value = ["address"], unique = true)]
)
data class DeviceEntity(
    @PrimaryKey(autoGenerate = true)
    val id: Int,
    var address: String,
    @ColumnInfo(name = "device_name", defaultValue = "")
    var deviceName: String,
    @ColumnInfo(defaultValue = "")
    var alias: String?,
    @ColumnInfo(name = "connection_state", defaultValue = "-1")
    var connectionState: Int,
    @ColumnInfo(name = "bond_state", defaultValue = "")
    var bondState: Int,
    @ColumnInfo(name = "serial_number", defaultValue = "")
    var serialNumber: String,
    @ColumnInfo(name = "battery_level", defaultValue = "-1")
    var batteryLevel: Int
)

fun DeviceEntity.asExternalModel() = Device(
    id = id,
    address = address,
    deviceName = deviceName,
    alias = alias,
    connectionState = connectionState,
    bondState = bondState,
    serialNumber = serialNumber,
    batteryLevel = batteryLevel
)

fun Device.asEntity() = DeviceEntity(
    id = id,
    address = address,
    deviceName = deviceName,
    alias = alias,
    connectionState = connectionState,
    bondState = bondState,
    serialNumber = serialNumber,
    batteryLevel = batteryLevel
)
